package com.cby.android.base.handler

import android.os.Bundle
import android.os.Looper
import android.util.Log
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.cby.android.base.R

/**
 * @description: Handler2Activity
 *
 * @author ChenBaoyang
 * @date 2021/10/18 16:44
 */
class Handler2Activity : AppCompatActivity() {

    private lateinit var showTv: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_handler2)
    }

    fun onChildThread(view: android.view.View) {
        ThreadHelper.handWork(object : ThreadHelper.WorkTask {
            override fun doWork() {
                Log.d("Handler2Activity", "F(${getNum(1000)})------")
                Log.d("Handler2Activity", "doWork Thread------" + Thread.currentThread())
            }

            override fun onFinish() {
                Log.d("Handler2Activity", "Child Thread------" + Thread.currentThread())
            }

        }, false, endMainThread = true)
    }

    fun onMainThread(view: android.view.View) {
        ThreadHelper.handWork(object : ThreadHelper.WorkTask {
            override fun doWork() {
                Log.d("Handler2Activity", "F(${getNum(1000)})------")
                Log.d("Handler2Activity", "doWork Thread------" + Thread.currentThread())
            }

            override fun onFinish() {
                Log.d("Handler2Activity", "Main Thread------" + Thread.currentThread())
            }

        }, true, endMainThread = false)
    }

    fun getNum(n: Int): Int {

        //返回-1时表示参数不合法
        if (n <= 0) {
            return -1
        }
        if (n <= 2) {
            return 1
        }
        var pre1 = 1
        var pre2 = 1
        var nvalue = 0
        if (n > 2) {
            for (i in 3..n) {
                nvalue = pre1 + pre2
                pre1 = pre2
                pre2 = nvalue
            }
        }
        return nvalue
    }
}